// pages/symNotice/symNotice.js
var app=getApp();
var util = require('../../utils/util.js');  
Page({

  /**
   * 页面的初始数据
   */
  data: {
    messageData:[],
    imageUrl: app.globalData.imageUrl,
    userInfo:'',
    loadSuccess:false,
    currentPage:1,
    hasData:true,
    loadMoreTxt: '正在加载数据。。。',
    loadMore: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo'))
    })
    this.getNotice(true);
    wx.showLoading();
  },
  getNotice:function(flag){
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'tGySystemMessage/page', //仅为示例，并非真实的接口地址
      data: {
        toUserId:this.data.userInfo.id,
        currentPage: this.data.currentPage,
        pageNumber:10
      },
      success: function (res) {
        wx.hideLoading();
        that.setData({
          loadSuccess: true
        })
        if (res.data.success) {
          if (res.data.data.page.totalNumber>0){
            if (res.data.data.systemMessageList.length < 10) {
              that.setData({
                  hasData: false
                })
              } else {
              that.setData({
                  hasData: true
                })
              }
            res.data.data.systemMessageList.forEach(function (ele) {
              if (ele.sendTime) {
                ele.time = util.getDate(ele.sendTime);
              }
            })
            if (flag) {
              wx.hideNavigationBarLoading() //完成停止加载
              wx.stopPullDownRefresh() //停止下拉刷新
              that.setData({
                messageData: []
              });
            } else {
              that.setData({
                loadMore: false
              });
            }
            that.data.messageData = that.data.messageData.concat(res.data.data.systemMessageList);
            that.setData({
              messageData: res.data.data.systemMessageList
            })
          }
        }
      }
    })
  },
  showDetail:function(e){
    var obj=e.currentTarget.dataset.obj;
    if (obj.block==1){//打赏
      wx.navigateTo({
        url: '/pages/myReward/myReward',
      })
    } else if (obj.block == 2){
      // carMine
      wx.navigateTo({
        url: '/pages/carMine/carMine',
      })
    } else if (obj.block == 3) {
      wx.navigateTo({
        url: '/pages/myOrder/myOrder',
      })
    }else{
      wx.navigateTo({
        url: '/pages/myRegistration/myRegistration?isMine=true',
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    this.setData({
      currentPage: 1
    })
    this.getNotice(true);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (that.data.hasData) {
      this.setData({
        loadMoreTxt: '正在加载数据。。。',
        loadMore: true,
        currentPage: this.data.currentPage + 1
      });
      this.getNotice();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})